package edu.jptest.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

import edu.jptest.bean.Group;
import edu.jptest.serviceImp.GroupService;

public class GroupAction extends ActionSupport {
	
	private static final long serialVersionUID = -6984538490646081304L;
	private Map<String, List<String>> groupMap;
	private List<String> groupList;
	private List<String> classList;
	private List<Group> groupClassList;

	public Map<String, List<String>> getGroupMap() {
		return groupMap;
	}

	public void setGroupMap(Map<String, List<String>> groupMap) {
		this.groupMap = groupMap;
	}

	public List<String> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<String> groupList) {
		this.groupList = groupList;
	}

	// 根据团体名称得到班级名称
	@SuppressWarnings("unchecked")
	public List<String> getClassList() {
		ValueStack stack = ServletActionContext
				.getValueStack(ServletActionContext.getRequest());
		Object groups = stack.findValue("top");
		if (groups != null) {
			classList = (List<String>) groupMap.get(groups);
			return classList;
		} else {
			return Collections.EMPTY_LIST;
		}
	}

	public void setClassList(List<String> classList) {
		this.classList = classList;
	}

	public List<Group> getGroupClassList() {
		return groupClassList;
	}

	public void setGroupClassList(List<Group> groupClassList) {
		this.groupClassList = groupClassList;
	}

	// 得到团体映射
	public String getGroupClass() {
		GroupService groupService = new GroupService();
		groupClassList = groupService.getAllGroups();
		Iterator<Group> groupIterator = groupClassList.iterator();

		groupList = new ArrayList<String>();
		groupMap = new HashMap<String, List<String>>();
		while (groupIterator.hasNext()) {
			Group group = (Group) groupIterator.next();
			if (!groupList.contains(group.getGroupName())) {
				groupList.add(group.getGroupName());
				List<String> classes = new ArrayList<String>();
				classes.add(group.getClassName());
				groupMap.put(group.getGroupName(), classes);
			} else {
				List<String> classes = groupMap.get(group.getGroupName());
				if (classes == null) {
					List<String> classes2 = new ArrayList<String>();
					classes2.add(group.getClassName());
					groupMap.put(group.getGroupName(), classes2);
				} else {
					classes.add(group.getClassName());
					groupMap.put(group.getGroupName(), classes);
				}
			}
		}
		return SUCCESS;
	}
}
